from Common.log import logger1
import os
import unittest
from HTMLTestRunner import HTMLTestReportCN
import time
from Common.sendEmail import sendEmail

def create_suite():
    #获取testcase路径
    case_dir=os.path.dirname(__file__)+r'/testCase'
    #查找测试用例，生成测试套件
    #实例化TestLoader
    loader=unittest.TestLoader()
    #调用discover方法
    suite=loader.discover(start_dir=case_dir,pattern='Test*.py')
    #查看套件
    logger1.debug(f'创建好的测试套件{suite}')
    #返回测试套件
    return suite

def auto_clear():
    dir=os.path.dirname(__file__)+r'/testReport/'
    #获取报告下所有的文件，得到一个列表
    dirlist1=os.listdir(dir)
    time_list1=[]
    for i in dirlist1:
        time1=os.path.getctime(dir+i)
        time2=time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(time1))
        time_list1.append(time2)
    file_dict1={dirlist1[j]:time_list1[j] for j in range(len(dirlist1))}
    logger1.debug(file_dict1)

if __name__ == '__main__':
    suite1=create_suite()
    cute_time=time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime())
    report=os.path.dirname(__file__)+r'/testReport/'+cute_time+'_report.html'
    with open(report,'wb') as f:
        runner=HTMLTestReportCN(title="接口测试报告",stream=f,description="接口测试")
        runner.run(suite1)
    se=sendEmail()
    time.sleep(1)
    se.send_mail_html(report)
    se.send1(report)
    # auto_clear()
